package com.guaoran.micro.services.spring.cloud.config.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Set;

/**
 * @author : gucheng
 * @Description : <br/>
 * @date :2018/12/1 15:13
 */
@SpringBootApplication
public class SpringCloudConfigClientApplication {
    private final ContextRefresher contextRefresher;

    private final Environment environment;

    public SpringCloudConfigClientApplication(ContextRefresher contextRefresher,
                                                  Environment environment) {
        this.contextRefresher = contextRefresher;
        this.environment = environment;
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigClientApplication.class,args);
    }


    /**
     * 定时调用refresh()
     * 或者调用/refresh
     */
//    @Scheduled(fixedRate = 5 * 1000, initialDelay = 3 * 1000)
//    public void autoRefresh() {
//
//        Set<String> updatedPropertyNames = contextRefresher.refresh();
//
//        updatedPropertyNames.forEach( propertyName ->
//                System.err.printf("[Thread :%s] 当前配置已更新，具体 Key：%s , Value : %s \n",
//                        Thread.currentThread().getName(),
//                        propertyName,
//                        environment.getProperty(propertyName)
//                ));
//    }
}
